package com.yswy.backstage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.service.AppProjectService;
import com.yswy.backstage.service.SoftnessService;
import com.yswy.backstage.service.UserProjectService;
import com.yswy.common.utils.ListUtils;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.*;
import com.yswy.domain.entity.user.User;
import com.yswy.domain.framework.context.CurrentUserContext;
import com.yswy.domain.mapper.AppProjectMapper;
import com.yswy.domain.mapper.SoftnessMapper;
import com.yswy.domain.vo.dto.BaseFilter;
import com.yswy.domain.vo.gld.addOrUpate.AppProjectAddOrUpdate;
import com.yswy.domain.vo.gld.addOrUpate.SoftnessAddOrUpdate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

import static com.yswy.backstage.utils.CrudUtils.selectCount;
import static com.yswy.common.constants.WarnMsgConstants.NULL_MSG;

@Slf4j
@Service
public class SoftnessServiceImpl extends ServiceImpl<SoftnessMapper, Softness>
        implements SoftnessService {


    @Override
    public  Page<Softness> listSoftness(BaseFilter req) {
        Page<Softness> Softnesss = baseMapper.pageSoftness(req
                , new Page<>(req.getPageNum() , req.getPageSize()) );
        return Softnesss;
    }

    @Override
    public List<Softness> AllSoftness() {
        return list(new LambdaQueryWrapper<Softness>()
                .eq(BasePojo::getIsDel , 0));
    }

    @Override
    public Boolean isExist(String softness, String softnessNo) {
        Integer count = count(new LambdaQueryWrapper<Softness>()
                .eq(Softness::getSoftness , softness)
                .eq(Softness::getSoftnessNo , softnessNo)
                .eq(BasePojo::getIsDel , 0));
        if (count>0){
            return true ;
        }
        return false;
    }

    @Override
    public void addOrUpadteSoftness(SoftnessAddOrUpdate softnessAddOrUpdate) {
        User user = CurrentUserContext.getCurrentUser();
        Softness softness = null ;
        Boolean isExist =  isExist(softnessAddOrUpdate.getSoftness(), softnessAddOrUpdate.getSoftnessNo());

        if (softnessAddOrUpdate.getId() != null){
            softness = getById(softnessAddOrUpdate.getId());
            Assert.state(null != softness && null != softness.getId(), NULL_MSG);
            BeanUtil.copyProperties(softnessAddOrUpdate, softness);
            BasePojo.setBase(user.getId().longValue(), false, softness);
            updateById(softness);
        }else {
            if (isExist){
                throw new IllegalStateException("软著已存在");
            }
            softness = new Softness(softnessAddOrUpdate , user);
            save(softness);
        }
    }



    @Override
    public  void delSoftness(Long softnessId) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, softnessId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        Softness bo = new Softness();
        bo.setId(softnessId);
        bo.setIsDel(1);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        updateById(bo);
    }

}
